package com.wujie.activies.service.helper;

import com.wujie.activies.model.Activity;
import com.wujie.activies.model.Contact;
import org.apache.log4j.Logger;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

import org.jxls.reader.ReaderBuilder;
import org.jxls.reader.XLSReader;

import org.xml.sax.SAXException;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Leonid Vysochyn on 6/24/2015.
 */
public class XlsReaderDemo {
    static Logger logger = Logger.getLogger(XlsReaderDemo.class);

    private static String wujiedata = "wujie_data.xls";
    private static final String wujiexml = "wujie.xml";

    public static void main(String[] args) throws IOException, SAXException, InvalidFormatException {
        logger.info("Running XLS Reader demo");
        executewujie();
    }


    public static void executewujie() throws IOException, SAXException, InvalidFormatException {
        logger.info("Reading xml config file and constructing XLSReader");
        try(InputStream xmlInputStream = XlsReaderDemo.class.getResourceAsStream("/dep/tpl/"+wujiexml)) {
            XLSReader reader = ReaderBuilder.buildFromXML(xmlInputStream);
            try (InputStream xlsInputStream = XlsReaderDemo.class.getResourceAsStream("/dep/tpl/"+wujiedata)) {
                List<Contact> departments = new ArrayList<>();
                Map<String, Object> beans = new HashMap<>();
                Activity activity = new Activity();
                Map<String,Object> test = new HashMap<>();
                test.put("name","");
                beans.put("contacts", departments);
                beans.put("activies",test);
                logger.info("Reading the data...");
                reader.read(xlsInputStream, beans);
                for (Contact contact : departments) {
                    logger.info(contact.getName() + ": " + contact.getPhone());
                }
                logger.info("\n title: "+test.get("name"));
            }
        }
    }
}
